<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>运动员信息管理</title>
</head>
<link rel="stylesheet" href="../res/layui/css/layui.css" media="all">
<script src="../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../res/layui/layui.js"></script>
<body>
    <table class="layui-hide" id="tab" lay-filter="test"></table>
</body>
<script type="text/javascript">
    $.ajaxSetup({
        async:false
    });
</script>
<script type="text/javascript">
    //检查登陆状态
    function findLogin(tableTwo){
        $.ajax({
            url:"../AtsEmpAthleteAction/findLogin",
            type:"post",
            //traditional:true,
            dataType:"json",
            // contentType: "application/json;charset=utf-8",
            success:function(data){
                if(data.num==0){
                    layer.msg(data.msg,{
                        icon:5,
                        time:1000 //1秒关闭(如果不配置默认三秒)
                    },function(){
                        //刷新父页面
                        parent.location.reload();
                        //跳转
                        $(location).attr('href','../jurisdiction/Pages/index.html');
                    });
                }else{
                    //刷新表格
                    tableTwo.reload();
                }

            }
        });
    }
    var table =null;
    var tableTwo = null;
    layui.use('table', function(){
        table = layui.table;
        tableTwo = table.render({
            elem: '#tab'
            ,height: 570
            ,url: '../AtsEmpAthleteAction/findAllX' //数据接口
            ,page: true //开启分页
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[ //表头
               {type: 'checkbox', fixed: 'left'}
                //,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left', totalRowText: '合计：'}
                ,{field: 'athleteName', title: '运动员姓名', width:130}
                ,{field: 'deptName', title: '部门名称', width: 130}
                ,{field: 'postName', title: '岗位名称', width:130}
                ,{field: 'athleteSex', title: '运动员性别', width: 130}
                ,{field: 'organName', title: '注册单位', width:130}
                ,{field: 'nativePlace', title: '籍贯', width: 130}
                ,{fixed: 'right', width: 240, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //查询是否登陆
        findLogin(tableTwo);
        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            console.info(data.athleteId);
            if(layEvent === 'edit'){ //编辑
                layer.open({
                    type: 2,
                    title:'用户修改页面',
                    shadeClose:true,
                    shade:0.8,
                    area:['80%','80%'],
                    content: ['AtsEmpAthleteUpdateOrAdd.html?id='+data.athleteId], //指定内容为某个页面
                    end:function(){
                        //刷新表格
                        tableTwo.reload();
                    }
                });
            }else if(layEvent == 'del'){
                layer.confirm('确定要删除本条记录吗？', {
                    btn: ['确定','取消'] //按钮
                }, function(){
                    $.ajax({
                        url:"../AtsEmpAthleteAction/deleteExpertDate",
                        type:"post",
                        data:{
                            id:data.athleteId
                        },
                        dataType:"json",
                        // contentType: "application/json;charset=  utf-8",
                        success:function(data){
                            layer.msg(data.msg,{
                                icon:1,
                                time:1000 //1秒关闭(如果不配置默认三秒)
                            },function(){
                                //刷新本页面
                                location.reload(true);
                            });
                        },error:function(){
                            alert("服务器出现异常，请稍后");
                        }
                    });
                });
            }else if(layEvent == 'info'){
                //查看某人的全部信息
                layer.open({
                    type: 2,
                    title:'更多信息',
                    shadeClose:true,
                    shade:0.8,
                    area:['80%','80%'],
                    content: ['AtsEmpAthleteUpdateOrAdd.html?state=1&id='+data.athleteId], //指定内容为某个页面
                    end:function(){
                        //刷新表格
                        //tableTwo.reload();
                    }
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var data = obj.data;
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            //alert(obj.event);
            switch(obj.event){
                //增加
                case 'add':
                    layer.open({
                        type: 2,
                        title:'运动员新增管理',
                        shadeClose:true,
                        shade:0.8,
                        area:['80%','80%'],
                        content: ['AtsEmpAthleteUpdateOrAdd.html'], //数组第二项即吸附元素选择器或者DOM
                        end:function(){
                            //刷新表格
                            tableTwo.reload();
                        }
                    });
                    break;
                //批量删除
                case 'deletes':
                    //alert(data.length);
                    if(data.length == 0){
                        alert('至少选择一行数据删除哦~');
                    }else {

                        //alert(JSON.stringify(data[0].athleteId));
                        var ids = [];//存放相应的用户Id
                        for(var i=0; i<data.length;i++){
                            //alert(checkStatus.data[i].athleteId);
                            ids.push(checkStatus.data[i].athleteId);
                            //alert(ids[i]);
                        }
                        //发送ajax请求
                        $.ajax({
                            url:"../AtsEmpAthleteAction/deleteAtheleteDates",
                            type:"post",
                            traditional:true,
                            data:{
                                ids:ids
                            },
                            dataType:"json",
                            // contentType: "application/json;charset=utf-8",
                            success:function(data){
                                layer.msg(data.msg,{
                                    icon:1,
                                    time:1000 //1秒关闭(如果不配置默认三秒)
                                },function(){
                                    //刷新本页面
                                    location.reload(true);
                                });
                            }
                        });

                    }
                    //alert("出发了批量删除");

                    break;
                case 'selectName':
                    //alert('表格重载');
                    var athleteName = $("#athleteName").val();
                    //表格重载
                    tableTwo.reload({
                        url:'../AtsEmpAthleteAction/findAllX',
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            athleteName:$("#athleteName").val()
                        }
                    });
                    $("#athleteName").val(athleteName);
                    break;
            };

        });

    });
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="info">更多信息</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    <!-- 这里同样支持 laytpl 语法，如： -->
    {{#  if(d.auth > 2){ }}
    <a class="layui-btn layui-btn-xs" lay-event="check">审核</a>
    {{#  } }}
</script>
<script type="text/html" id="toolbarDemo">
    <div style="width: 100%;height: 30%;">
        <div style="position: absolute;left: 5%;top: 5%;">
            <button class="layui-btn" lay-event="add">新增运动员信息</button>
        </div>
        <div style="position: absolute;left: 25%;top: 5%;">
            <button class="layui-btn layui-btn-danger" lay-event="deletes">批量删除</button>
        </div>
        <div style="width: 100%;position: absolute;left: 40%;top: 5%;">
            <input type="text" id="athleteName" placeholder="试试教练员姓名？"style="width: 25%;" class="layui-input">
        </div>
        <div style="position: absolute;left: 65%;top: 5%;">
            <button class="layui-btn" lay-event="selectName" >搜索姓名</button>
        </div>

    </div>
</script>
</html>